/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.storage.android.sqlite;

import java.util.NoSuchElementException;

import android.database.Cursor;
import de.fuberlin.replication.library.Subset;
import de.fuberlin.replication.library.Version;
import de.fuberlin.replication.library.VersionVector;

/**
 * Android-specific SQLite {@link Subset} <code>Iterator</code> implementation.
 */
public class SQLiteSubsetIterator extends SQLiteIterator<Subset> {
	/**
	 * @see SQLiteIterator#SQLiteIterator(Cursor)
	 */
	public SQLiteSubsetIterator(Cursor cursor) {
		super(cursor);
	}

	@Override
	public Subset next() {
		if (!hasNext())
			throw new NoSuchElementException();

		Cursor cursor = getCursor();

		Subset subset = new Subset();
		subset.setId(cursor.getLong(0));

		VersionVector knowledge = subset.getKnowledge();

		do {
			String replica = cursor.getString(1);
			long version = cursor.getLong(2);

			if ((replica == null) || (version == 0))
				continue;

			knowledge.add(new Version(replica, version));
		} while (cursor.moveToNext() && (cursor.getLong(0) == subset.getId()));

		return subset;
	}
}